package com.ruoyi.project.channel.businessCashChange.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.project.channel.vwBusinessCashStat.domain.VwBusinessCashStat;
import com.ruoyi.project.channel.vwBusinessCashStat.service.IVwBusinessCashStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.channel.businessCashChange.mapper.BusinessCashChangeMapper;
import com.ruoyi.project.channel.businessCashChange.domain.BusinessCashChange;
import com.ruoyi.project.channel.businessCashChange.service.IBusinessCashChangeService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 资金变动 服务层实现
 * 
 * @author zero
 * @date 2019-05-18
 */
@Service
public class BusinessCashChangeServiceImpl implements IBusinessCashChangeService 
{
	@Autowired
	private BusinessCashChangeMapper businessCashChangeMapper;
	@Autowired
	private IVwBusinessCashStatService vwBusinessCashStatService;
	/**
     * 查询资金变动信息
     * 
     * @param cashId 资金变动ID
     * @return 资金变动信息
     */
    @Override
	public BusinessCashChange selectBusinessCashChangeById(Long cashId)
	{
	    return businessCashChangeMapper.selectBusinessCashChangeById(cashId);
	}
	
	/**
     * 查询资金变动列表
     * 
     * @param businessCashChange 资金变动信息
     * @return 资金变动集合
     */
	@Override
	public List<BusinessCashChange> selectBusinessCashChangeList(BusinessCashChange businessCashChange)
	{
	    return businessCashChangeMapper.selectBusinessCashChangeList(businessCashChange);
	}
	
    /**
     * 新增资金变动
     * 
     * @param businessCashChange 资金变动信息
     * @return 结果
     */
	@Override
	public int insertBusinessCashChange(BusinessCashChange businessCashChange)
	{
	    return businessCashChangeMapper.insertBusinessCashChange(businessCashChange);
	}

	/**
	 *
	 * @param amount
	 * @param businessId
	 * @param opType
	 * @param remark
	 * @return
	 */
	@Override
	public int insertBusinessCashChange(BigDecimal amount,Long businessId, String opType, String remark)
	{
		// 记录资金变更记录
		BusinessCashChange change = new BusinessCashChange();
		change.setAmount(amount);
		change.setBusinessId(businessId);
		change.setOpType(opType);
		VwBusinessCashStat searchState = new VwBusinessCashStat();
		searchState.setBusinessId(businessId);
		List<VwBusinessCashStat> stats = vwBusinessCashStatService.selectVwBusinessCashStatList(searchState);
		if(stats.size() == 0){
			change.setPreAmount(BigDecimal.ZERO);
		}else{
			change.setPreAmount(stats.get(0).getAmountSum());
		}
		change.setNextAmount(change.getPreAmount().add(change.getAmount()));
		change.setCreateTime(new Date());
		change.setRemark(remark);
		return businessCashChangeMapper.insertBusinessCashChange(change);
	}
	/**
     * 修改资金变动
     * 
     * @param businessCashChange 资金变动信息
     * @return 结果
     */
	@Override
	public int updateBusinessCashChange(BusinessCashChange businessCashChange)
	{
	    return businessCashChangeMapper.updateBusinessCashChange(businessCashChange);
	}

	/**
     * 删除资金变动对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteBusinessCashChangeByIds(String ids)
	{
		return businessCashChangeMapper.deleteBusinessCashChangeByIds(Convert.toStrArray(ids));
	}
	
}
